var addNum = 0,nowNum = 0,itemHeight = 46,first = true,is_animat = true,isSend = false;
var tempChat = [],onceMax = refreshtime/1000,seeIndex = overTime+1,beijintime = now/1000;
var cmlist = document.getElementById("chat"),cmchat = $("#chat");

var is_send = false;

var card_id = 0;

(function(){
	var winHeight = $(window).height();
	$('.cm-wrapper').height(winHeight + 'px');
	$('.alert-wrapper .icon-close').click(function(){
		$(this).parent().parent().addClass('hide');
	});
	//立即兑换弹框
	$('#duihuan').click(function(){
		
		
		
		 var saveUrl = "index.php?g=mobile&m=card&a=chatCard";
		 
		
		
		$.post(saveUrl,"",function(res){
			
			
			 
			if(res.error==0){
				
				card_id = res.data.id;
				
				$("#card_name").html(res.data.description);
				$("#integral").html(res.data.change_integral);
				
				$('.duihuan-alert-wrapper').removeClass('hide');
			}
			else
			{
				warn(res.content);
			}
		},"json");
		
		
		
		
	});
	
	$('#jifen').click(function(){
		$('.jifen-alert-wrapper').removeClass('hide');
	});
	
	$("#cancel").click(function(){
		$(this).parent().parent().parent().addClass('hide');
	})
	getChatJson();
	move();
})();


function info(){
	
	$('.duihuan-alert-wrapper').addClass('hide');
	
	
	var cardId = card_id;
	
	//alert(cardId);
	
	ajaxCard(cardId);

};
function yhqInfo(){
	
	var cardId = card_id;
	
	saveInfo(cardId);
	
};

function move(){ 
	itemScale = setInterval(function(){
		beijintime++;
		var chat = tempChat[seeIndex-1];
//		console.info(seeIndex);
		//正常弹幕
		if(chat!=undefined&&chat!=null&&chat!=""){//正常弹幕执行
			if(chat.status=="immediately"){
				var html ="<div class='cm-item'> " +
								"<span class='cm-r tranScale'>" +
									"<img src='/ws/Mobile/Webroot/gf/images/chat/share.png' alt=''>" +
										chat.comment+
								"</span>" +
							"</div>";
			}else{
				var html ="<div class='cm-item'> " +
							"<span class='cm-l tranScale'>" +
								"<img src='"+chat.headimgurl+"' alt=''>" +
									chat.comment+
								"</span>" +
							"</div>";
			}
			
			animat(html);
		}
		//特殊弹幕
		var special = getSpecialJson();
		if(special.length>0){
			for(var p in special){
				var specialHtml ="<div class='cm-item special'> " +
									"<span onClick=\"toSpecialUrl(this,\'"+special[p].source_url+"\')\" class='cm-r tranScale'>" +
										"<img src='"+special[p].pic_url+"' alt=''>" +
											special[p].title+
									"</span>" +
								 "</div>";
				animat(specialHtml);
			}
		}
		if(seeIndex>=onceMax){
			seeIndex = 1;
			getChatJson();
		}else{
			seeIndex++;
		}
	}, 1000);
};
function stopMove(){
	clearInterval(itemScale);
}
//主要防止高度混乱
function animat(html){
	if(is_animat){
		is_animat = false;
		cmchat.append(html);
		var h = cmlist.offsetHeight;
		if(!first){
			h = h + itemHeight;
		}else{
			first = false;
		}
		
		if(h<689){
			cmchat.animate({height:h+"px"},600,function(){
				is_animat = true;
			});
		}else{
			$('.cm-item').eq(0).animate({height:"0px"},600,function(){
				var _this = $(this);
				_this.remove();
				is_animat = true;
			});
		}
//		cmchat.animate({height:h+"px"},'',function(){
//			is_animat = true;
//		});
	}else{
		setTimeout(function(){
			animat(html);
		},300);
	}
	
}
function getChatJson(){
	tempChat = [];
	var jsonurl = "chatR.json?"+Math.random();
	$.getJSON(jsonurl, function(data){
		tempChat = data;
	})
}

function  getSpecialJson(){
	var SpecialJson = [];
	for(var o in rule){
		if(rule[o].showTime==beijintime){
			SpecialJson.push(rule[o]);
		}
	}
	return SpecialJson;
}

// 添加评论
function addCmItem(){
	//发评论
		if(isSend){
			return;
		}
		isSend =true;
		var text = $("#cmText").val();
		if(text==null||text==""||text==undefined){
			warn("请吐槽，谢谢~");
			isSend = false;
			return false;
		}
		var length = getBLen(text);
		if(length>chatMaxNum){
			warn("吐槽点太多啦，少点字吧~");
			isSend = false;
			return false;
		}
		var commentTimestamp  = Date.parse(new Date());//控制多次评论
		if((commentTimestamp-now)/1000<60){
				if(number>=commentMinuteMax){
					warn("不要经常吐槽，这样不好~");
					isSend = false;
					return false;
				}
		}else{
			now = commentTimestamp;
			number = 0;//重新计算
		}
		if(commentDayNum>=commentDayMax){
			warn("今天的吐槽点已经很多，请明天在继续~");
			isSend = false;
			return false;
		}
		var url = "index.php?g=mobile&m=rchat&a=saveComment"
		var data = {
			"text":text
		};
		$.post(url,data,function(data){
			isSend =false;
			if(data.error=="1"){
				number++;
				commentDayNum++;
				$("#cmText").val("");
				
				$("#userIntegral").html(data.integral);
				var html ="<div class='cm-item'> " +
								"<span class='cm-r tranScale'>" +
									"<img src='"+imgurl+"' alt=''>" +
										text+
									"</span>" +
							"</div>";
				animat(html);
				if(data.content){
					var content = eval(data.content);
					for (var o in content) {
						var html = "<div class='cm-item special'> " +
										"<span onClick=\"toSpecialUrl(this,\'"+content[o].source_url+"\')\" class='cm-r tranScale'>" +
											"<img src='"+content[o].pic_url+"' alt=''>" +
												content[o].title+
										"</span>" +
									"</div>";
						animat(html);
					}
				}
			}else if(data.error=="4004"){
				warn("您已经被管理员禁言了~");
			}else if(data.error=="2002"){
				warn("您的吐槽有敏感词汇，无法发送~");
			}else{
				warn("吐槽失败~");
			}
		},"json");
}
function getBLen(str) {
  if (str == null) return 0;
  if (typeof str != "string"){
    str += "";
  }
  return str.replace(/[^\x00-\xff]/g,"00").length;
}

function toSpecialUrl(params,url){
	if(url!=undefined&&url!=null&&url!="")
		window.location.href=url;
}

//不打断alert
function warn(msg,opt,left,top){
	if(opt){
	 	var obj = $("#"+opt);
	}
 	new Toast({context:$('body'),message:msg},obj,left,top).show();
}
var Toast = function(config,obj,left,top){
	this.context = config.context==null?$('body'):config.context;//上下文
	this.message = config.message;//显示内容
	this.time = config.time==null?5000:config.time;//持续时间
	this.left = config.left;//距容器左边的距离
	this.top = ($(document).height()/4)*3;//距容器上方的距离
	if(obj){ 
	this.left = obj.offset().left + left;
	this.top = obj.offset().top + top;
	}
	this.init();
}
var msgEntity;
Toast.prototype = {
	//初始化显示的位置内容等
	init : function(){
		$("#toastMessage").remove();
		//设置消息体
		var msgDIV = new Array();
		msgDIV.push('<div id="toastMessage">');
		msgDIV.push('<span>'+this.message+'</span>');
		msgDIV.push('</div>');
		msgEntity = $(msgDIV.join('')).appendTo(this.context);
		//设置消息样式
		var left = this.left == null ? this.context.width()/2-17-msgEntity.find('span').width()/2 : this.left;
		var top = this.top == null ? '20px' : this.top;
		msgEntity.css({position:'absolute',top:top,'z-index':'9999',left:left,'background-color':'#2a70e6',color:'white','font-size':'14px',padding:'12px',margin:'5px','border-radius':'4px','-moz-border-radius':'4px','-webkit-border-radius':'4px','font-family':'微软雅黑'});
		//msgEntity.addClass(".toast");
		msgEntity.hide();
 	},
	//显示动画
	show :function(){
		msgEntity.fadeIn(this.time/2);
		msgEntity.fadeOut(this.time/2);
	}
}